$(function(){

let id =  location.search.split('=')[1];

$.ajax({
url:'../interface/getitem.php',
type:'get',
data: { id },
dataType: 'json'
}).then(res =>{
let pic=JSON.parse(res.picture);


$('.price').html(`${res.price}`);

$('.middle>.title').html(`${res.title}`)

$('.img>a').html(`<img src="./${pic[0].src}" />`);

let s = `./${pic[0].src}`

$('.big>.big2>img').attr({src:s})


let sel = $('.left>ul>li>a>img')


for( var i=0; i<pic.length;i++){

let num = `./${pic[i].src}`;

sel.eq(i).attr({src:num})
}

$('.second>p').html(`${res.details}`);




$('#additem').on('click',function(){

addItem(res.id,$('#num').val())

});



}).catch(xhr => {
    console.log(xhr.status);
  });



});

function addItem(id,num){
  let product1 = {id,num};

  let shop = cookie.get('shop');

  if(shop){

  shop= JSON.parse(shop);
  
  if(shop.some(el=>el.id==id)) {

  let index = shop.findIndex(elm=>elm.id==id);
  let count = parseInt(shop[index].num);
  count += parseInt(num);
  shop[index].num = count;
  }else{
    shop.push(product1)
  }

  }else{
    shop = [];
    shop.push(product1);
  }

  cookie.set('shop',JSON.stringify(shop));
}

